package com.qf.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsBrandMapper;
import com.qf.pojo.DtsBrand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * @author fy
 * @version V1.0
 * @Project workspace
 * @Package com.qf.service
 * @Description:
 * @Date 2022/6/21 17:50
 */
@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private DtsBrandMapper brandMapper;

    @Override
    public IPage<DtsBrand> queryBrandPage(Integer id, String name, Integer page, Integer limit, String sort, String order) {
        //1.创建分页对象
        IPage<DtsBrand> iPage = new Page<>(page,limit);
        //2.封装查询对象
        QueryWrapper<DtsBrand> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", false);
        //id不为空，校验
        if (!StringUtils.isEmpty(id)){
            queryWrapper.eq("id", id);
        }
        //name不为空，校验
        if (!StringUtils.isEmpty(name)){
            queryWrapper.like("name", name);
        }
        //排序
        if (!StringUtils.isEmpty(sort)&&!StringUtils.isEmpty(order)){
            //排序方式是否为升序
            if (order.equalsIgnoreCase("asc")){
                queryWrapper.orderByAsc(sort);
            }else {
                //降序
                queryWrapper.orderByDesc(sort);
            }

        }


        //3.查询并返回结果
//        IPage<DtsStorage> resultPage = storageMapper.selectPage(iPage, queryWrapper);
        IPage<DtsBrand> resultPage = brandMapper.selectPage(iPage, queryWrapper);


        return resultPage;

    }

    @Override
    public DtsBrand add(DtsBrand brand) {
        brand.setAddTime(new Date());
        brand.setUpdateTime(new Date());
        brandMapper.insert(brand);
        return brand;
    }

    @Override
    public int update(DtsBrand brand) {
        //设置修改时间
        brand.setUpdateTime(new Date());
        int count = brandMapper.updateById(brand);
        return count;
    }

    @Override
    public int delete(Integer id) {
        QueryWrapper<DtsBrand> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        int count = brandMapper.delete(queryWrapper);
        return  count;

    }
}
